package com.fortune.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fortune.common.paging.PagingData;
import com.fortune.dao.MstoreTransDao;
import com.fortune.dto.in.mStoreTrans.MStoreTransAListDto;
import com.fortune.dto.in.mStoreTrans.MStoreTransAddDetailDto;
import com.fortune.dto.in.mStoreTrans.MStoreTransAddDto;
import com.fortune.dto.in.mStoreTrans.MStoreTransEditDto;
import com.fortune.entity.MUserInfo;
import com.fortune.service.MStoreTransService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.mStoreTrans.MStoreTransInfo;
import com.fortune.vo.mStoreTrans.MstoreTransDetail;
@Service
public class MStoreTransServiceImpl implements MStoreTransService{
	@Autowired
	private MstoreTransDao md;
	@Override
	public PagingData<MStoreTransInfo> pagingData(MUserInfo mUserInfo, MStoreTransAListDto listDto) {
		List<MStoreTransInfo> list = md.list(mUserInfo, listDto);
		int count = md.count(mUserInfo, listDto);
		PagingData<MStoreTransInfo> pagingData = new PagingData<>(list,count);
		return pagingData;
	}
	@Override
	public MstoreTransDetail mstoreTransDetail(MUserInfo mUserInfo, String id) {
		MstoreTransDetail mstoreTransDetail = md.detail(mUserInfo, id);
		return mstoreTransDetail;
	}
	@Override
	@Transactional
	public int add(MUserInfo mUserInfo, MStoreTransAddDto addDto) {
		String uuid = UUIDUtil.get32UUID();
		int add = md.add(uuid, mUserInfo, addDto);
		if (add==1) {
			for (MStoreTransAddDetailDto addDetailDto : addDto.getList()) {
				md.addDetail(uuid, mUserInfo, addDetailDto);
			}
			return add;
		}
		return 0;
	}
	@Override
	@Transactional
	public int edit(MUserInfo mUserInfo, MStoreTransEditDto editDto) {
		int edit =  md.edit(mUserInfo, editDto);
		if (edit==1) {
			int delete = md.deleteDetail(mUserInfo, editDto);
			String uuid = editDto.getId();
			for (MStoreTransAddDetailDto addDetailDto : editDto.getList()) {
				md.addDetail(uuid, mUserInfo, addDetailDto);
			}
			return edit;
		}
		return 0;
	}
	@Override
	public int delete(MUserInfo mUserInfo, String id) {
		int delete = md.delete(mUserInfo, id);
		return delete;
	}
	@Override
	public int outConfirm(MUserInfo mUserInfo, String id) {
		int result = md.outConfirm(mUserInfo, id);
		return result;
	}
	@Override
	public int inConfirm(MUserInfo mUserInfo, String id) {
		int result = md.inConfirm(mUserInfo, id);
		return result;
	}

}
